<?php
/**
 * Created by PhpStorm.
 * User: Orange
 * Date: 2018/4/26
 * Time: 10:11
 */

namespace app\admin\controller\user;

use app\common\controller\Backend;
use app\common\utils\Constant;

/**
 * 用户列表
 * Class UserList
 * @package app\admin\controller\user
 */
class UserList extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }


    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->field(['password', 'salt', 'token'], true)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 修改用户成为会员
     * @param string $ids
     * @return string
     */
    public function change_type($ids = '')
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isAjax()) {
            $data = $this->request->post('row/a');
            $data['is_bind'] = 1;
            if (!check_mobile($data['phone'])) {
                $this->error('请输入正确的手机号');
            }
            $member = model('Member')
                ->where(array('phone' => $data['phone']))
                ->where(array('is_delete' => 0))
                ->find();
            if ($member) {
                $this->error('手机号已存在');
            }
            if (strlen($data['password']) > 12 || strlen($data['password']) < 6) {
                $this->error('密码长度应在6到12位之间');
            }
            $data['user_id'] = $ids;
            if ($data) {
                model('Member')->create($data);
                $this->model->where(array('id' => $ids))->update(array('user_type' => Constant::USER_TYPE_MEMBER));
                $this->success('修改成功');
            }
            $this->error('修改失败');
        }
        return $this->view->fetch();
    }

    public function search_log()
    {
        return $this->view->fetch();
    }

    public function being_admin()
    {
        $id = $this->request->post('id');
        if (!$id)
            return json(['status' => 0, 'msg' => "id不能为空"]);
        $this->model->where(array('id' => $id))->update(array('is_manager' => Constant::MANAGER));
        return json(['status' => 1, 'msg' => "修改成功"]);
    }

    public function cancel_admin()
    {
        $id = $this->request->post('id');
        if (!$id)
            return json(['status' => 0, 'msg' => "id不能为空"]);
        $this->model->where(array('id' => $id))->update(array('is_manager' => Constant::NOT_MANAGER));
        return json(['status' => 1, 'msg' => "修改成功"]);
    }

}